package com.qd.panda.service.stroke;

import com.cdqidi.util.DateUtil;
import com.cdqidi.util.LockTemplate;
import com.fasterxml.jackson.databind.JsonNode;
import com.qd.common.panda.domain.entity.stroke.PayResult;
import com.qd.common.panda.domain.entity.wallet.user.UserWalletRefundLogDTO;
import com.qd.panda.model.UserStrokeOrderRefund;
import com.qd.panda.service.stroke.order.UserStrokeOrderRefundService;
import com.qd.panda.service.wallet.user.UserWalletRefundApplyOrderService;
import com.qd.panda.service.wallet.user.UserWalletRefundApplyService;
import com.qd.panda.service.wallet.user.UserWalletRefundLogService;
import com.qd.panda.service.wallet.user.WalletService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author sjk
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class PayResultService {
    private final UserStrokeOrderRefundService userStrokeOrderRefundService;
    private final UserStrokeHolderService userStrokeHolderService;
    private final UserWalletRefundApplyOrderService userWalletRefundApplyOrderService;
    private final UserWalletRefundApplyService userWalletRefundApplyService;
    private final WalletService walletService;
    private final UserWalletRefundLogService userWalletRefundLogService;

    /**
     * 订单支付
     *
     * @param result 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public void payOrder(PayResult result) {
        JsonNode attach = result.getAttach();
        if (null == attach) {
            return;
        }
        int orderType = attach.get("orderType").intValue();
        String bid = result.getThirdPartyOrder();
        Integer payStatus = result.isSuccess() ? DisplayUtil.PAY_STATUS_1 : DisplayUtil.PAY_STATUS_2;
        userStrokeHolderService.afterPay(bid, payStatus, orderType);
    }

    /**
     * 退款
     *
     * @param result 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public void refundOrder(PayResult result) {
        UserStrokeOrderRefund refund = userStrokeOrderRefundService.getUniqueByBidAndRefundNo(result.getThirdPartyOrder(), result.getRefundNo());
        if (null == refund) {
            log.info("退款单不存在,bid: {},no: {}", result.getThirdPartyOrder(), result.getRefundNo());
            return;
        }
        refund.setRefundStatus(result.isSuccess() ? DisplayUtil.REFUND_STATUS_1 : DisplayUtil.REFUND_STATUS_2);
        if (result.isSuccess()) {
            refund.setRefundTime(DateUtil.strToLocalDateTime(result.getTimeEnd(), "yyyy-MM-dd'T'HH:mm:ss"));
        }
        userStrokeHolderService.afterRefund(refund.getSid(), refund.getRefundId(), refund.getRefundStatus());
        refund.freeData();
    }

    public void refundOtherOrder(PayResult result) {
        int status = result.isSuccess() ? DisplayUtil.REFUND_STATUS_1 : DisplayUtil.REFUND_STATUS_2;
        if (status == DisplayUtil.REFUND_STATUS_1) {
            //成功的不处理，因为默认就认为是成功的
            return;
        }
        String orderId = result.getThirdPartyOrder();


        UserWalletRefundLogDTO userWalletRefundLogDTO = userWalletRefundLogService.getByOrderIdAndNo(orderId, result.getRefundNo());
        if (null == userWalletRefundLogDTO) {
            log.error("没有查询到退款单,orderId: {}", orderId);
            return;
        }
        userWalletRefundLogDTO.setRefundStatus(status);


        LockTemplate.execute(userWalletRefundLogDTO.getUserId(), () -> {
            walletService.updateRefundStatus(userWalletRefundLogDTO);
            userWalletRefundLogDTO.freeData();
            return null;
        }, 60);
    }
}
